﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Configuration;

namespace WXYW
{
    public partial class SendProductNotification : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        private void InitialPage()
        {
            ApplicationService applicatonservice = new ApplicationService();
            ApplicationForm applicaton = applicatonservice.GetApplicationFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]));
            if (!applicaton.UserReference.IsLoaded)
            {
                applicaton.UserReference.Load();
            }
            if(!applicaton.ApplicationProduct.IsLoaded)
            {
                applicaton.ApplicationProduct.Load();
            }
            string products = string.Empty;
            foreach(ApplicationProduct product in applicaton.ApplicationProduct)
            {
                if(!product.ProductTypeReference.IsLoaded)
                {
                    product.ProductTypeReference.Load();
                }
                products += product.ProductType.ProductTypeName;
            }

            emailLabel.Text = applicaton.User.UserEmail;
            toLabel.Text = applicaton.User.UserName;
            bodyTextBox.Text = ConfigurationManager.AppSettings["SendProductEmailB"].ToString().Replace("{name}", applicaton.User.UserName).Replace("{rmacode}", applicaton.ApplicationCode).Replace("{product}", products).Replace("{sendtime}",DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
        }

        protected void sendLinkButton_Click(object sender, EventArgs e)
        {
            ApplicationService applicatonservice = new ApplicationService();
            ApplicationForm applicationForm = applicatonservice.GetApplicationFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]));
            applicationForm.NortificationDatetime = DateTime.Now;
            applicationForm.SendBackDateTime = DateTime.Now;
            applicationForm.SendWay = "B";
            applicatonservice.UpdateApplication(applicationForm);            
            ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('发送成功!');", true);
        }
    }
}
